package com.androidplex.bucketlist;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddItemActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_additem);

        Button submitButton = (Button)findViewById(R.id.button_new_item);
        final EditText text = (EditText) findViewById(R.id.newText);
        final EditText description = (EditText) findViewById(R.id.newDescription);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create and send an intent which gives the data to the main screen.
                Intent newItemIntent = new Intent(getApplicationContext(), ItemListActivity.class);
                newItemIntent.putExtra("new_name",text.getText().toString());
                newItemIntent.putExtra("new_description",description.getText().toString());

                setResult(ItemListFragment.RESULT_OK, newItemIntent);
                finish();
            }
        });

        Intent i = getIntent();
        if (i.getBooleanExtra("EDIT", false)) {
            text.setText(i.getStringExtra("NAME"));
            description.setText(i.getStringExtra("DESC"));
            submitButton.setText(R.string.edit_item_submit);
        } else {
            submitButton.setText(R.string.new_item_submit);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.add_item, menu);
        return true;
    }
    
}
